package model;

public enum JavaFileExtension {
	
	CLASS("class"),
	JAR("jar"),
	WAR("war"),
	EAR("ear");
	
	public static final String[] JAVA_FILE_EXTENSIONS;
	public static final String REGEX_FOR_JAVA_FILE_EXTS;
	public static final String REGEX_FOR_JAVA_FILES;
	
	static {
		JAVA_FILE_EXTENSIONS = getJavaFileExtensions();
		REGEX_FOR_JAVA_FILE_EXTS = getRegexForJavaFileExtensions();
		REGEX_FOR_JAVA_FILES = getRegexForJavaFiles();
	}
	
	private String ext;

	private JavaFileExtension(String ext) {
		this.ext = ext;
	}
	
	public String getExt() {
		return ext;
	}
	
	public static final JavaFileExtension getJavaFileExtension(String ext) {
		JavaFileExtension[] fileExtensions = JavaFileExtension.values();
		for (JavaFileExtension javaFileExtension : fileExtensions) {
			if (javaFileExtension.getExt().equals(ext)) {
				return javaFileExtension;
			}
		}
		
		return null;
	}
	
	private static final String[] getJavaFileExtensions() {
		
		JavaFileExtension[] constants = JavaFileExtension.values();
		String[] javaFileExtensions = new String[constants.length];
		for (int i = 0; i < constants.length; i++) {
			javaFileExtensions[i] = new String(constants[i].ext);
		}
		
		return javaFileExtensions;
	}
	
	public static final String getRegexForJavaFileExtensions() {
		String regex = "(";
		for (String ext : JAVA_FILE_EXTENSIONS) {
			regex += ext + "|";
		}
		regex = regex.substring(0, regex.length()-1);
		regex += ")$";
		
		return regex;
	}
	
	public static final String getRegexForJavaFiles() {
		return ".+\\." + getRegexForJavaFileExtensions();
	}

	public static boolean hasJavaFileExtension(String jarEntryName) {
		return jarEntryName.matches(REGEX_FOR_JAVA_FILES);
	}
	
}
